#include "pet.h"
#include <QTimer>
#include <QMouseEvent>

pet::pet(QWidget *parent)
    : QWidget(parent), roleLabel(new QLabel(this)), currentFrame(0)
{
    setWindowFlags(Qt::WindowType::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    this->installEventFilter(this);



    QTimer *updateTimer = new QTimer(this);
    updateTimer->callOnTimeout(this, &pet::updateRoleAnimation);
    updateTimer->start(500);

    updateRoleAnimation();
    roleLabel->resize(500,500);
}

pet::~pet()
{

}

void pet::updateRoleAnimation()
{
    QString qss("background-repeat:no-repeat;");
    roleLabel->setStyleSheet(qss+QString("background-image:url(:/new/prefix1/C:/Users/wHy/Pictures/QtResources/Stravinsky/%1.jpg)").arg(currentFrame));
    currentFrame = (currentFrame + 1) % 6 ;
}

bool pet::eventFilter(QObject *watched, QEvent *ev)
{
    QMouseEvent * mouseev = static_cast<QMouseEvent*>(ev);
    static QPoint begpos;
    if(ev->type() == QEvent::MouseButtonPress)
    {
        begpos = mouseev->globalPos() - this->pos();
    }

    else if(ev->type() == QEvent::MouseMove && mouseev->buttons()&Qt::MouseButton::LeftButton)
    {
        this->move(mouseev->globalPos() - begpos);
    }
    return false;
}

